﻿using System;
using System.Collections;
using System.Security.Permissions;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;

namespace SiteFox.WebParts.Blog
{
    [AspNetHostingPermission(SecurityAction.Demand,
      Level = AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand,
      Level = AspNetHostingPermissionLevel.Minimal)]

    public class CategoriesWP : WebPart
    {
        #region PublicProperies
        private int _BlogId = -1;
        [Personalizable(true)]
        public int BlogId
        {
            get { return _BlogId; }
            set { _BlogId = value; }
        }

        private string _ViewByCategoryUrl = "Default.aspx";
        [Personalizable(true)]
        public string ViewByCategoryUrl
        {
            get { return _ViewByCategoryUrl; }
            set { _ViewByCategoryUrl = value; }
        }

        private string _XsltUrl = "~/System/Design/xslt/WebParts/Blog/Categories.xslt";
        [Personalizable(true)]
        public string XsltUrl
        {
            get { return _XsltUrl; }
            set { _XsltUrl = value; }
        }
        #endregion

        protected override void CreateChildControls()
        {
            Controls.Clear();
            var _Categories = SiteFox.Business.Manager.BlogCategory.GetByBlog(_BlogId);
            Xml _Xml = new Xml();
            _Xml.TransformSource = _XsltUrl;
            XmlDocument _XmlDocument = new XmlDocument();
            XmlDeclaration _XmlDeclaration = _XmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
            XmlElement _XmlCategories = _XmlDocument.CreateElement("Categories");
            _XmlDocument.InsertBefore(_XmlDeclaration, _XmlDocument.DocumentElement);
            _XmlDocument.AppendChild(_XmlCategories);
            for (int i = 0; i < _Categories.Count; i++)
            {
                var _Category = _Categories[i];
                XmlElement _XmlCategory = _XmlDocument.CreateElement("Category");
                _XmlCategory.SetAttribute("Id", _Category.ID.ToString());
                _XmlCategory.SetAttribute("Url", _ViewByCategoryUrl + "?Category=" + _Category.ID.ToString());
                _XmlCategories.AppendChild(_XmlCategory);
                XmlElement _XmlCategoryTitle = _XmlDocument.CreateElement("Title");
                _XmlCategoryTitle.InnerText = _Category.Title;
                _XmlCategory.AppendChild(_XmlCategoryTitle);
                XmlElement _XmlCategoryDescription = _XmlDocument.CreateElement("Description");
                _XmlCategoryDescription.InnerText = _Category.Description;
                _XmlCategory.AppendChild(_XmlCategoryDescription);
            }
            _Xml.DocumentContent = _XmlDocument.InnerXml;
            Controls.Add(_Xml);
        }
        public override EditorPartCollection CreateEditorParts()
        {
            ArrayList _EditorArray = new ArrayList();
            Blog_Editor _EditorPart = new Blog_Editor();
            _EditorPart.ID = this.ID + "_Blog";
            _EditorArray.Add(_EditorPart);
            EditorPartCollection _EditorParts = new EditorPartCollection(_EditorArray);
            return _EditorParts;
        }

        // Create a custom EditorPart to edit the WebPart control.
        [AspNetHostingPermission(SecurityAction.Demand,
          Level = AspNetHostingPermissionLevel.Minimal)]
        private class Blog_Editor : EditorPart
        {
            Literal _lit_Brake;
            Label _lab_BlogList;
            DropDownList _ddl_BlogList;
            Label _lab_ViewByCategoryUrl;
            TextBox _tb_ViewByCategoryUrl;
            Label _lab_XsltUrl;
            TextBox _tb_XsltUrl;
            
            public override bool ApplyChanges()
            {
                CategoriesWP _part = (CategoriesWP)WebPartToEdit;
                _part._BlogId = Convert.ToInt32(_ddl_BlogList.SelectedValue);
                _part._ViewByCategoryUrl = _tb_ViewByCategoryUrl.Text;
                _part._XsltUrl = _tb_XsltUrl.Text;
                return true;
            }
            public override void SyncChanges()
            {
                EnsureChildControls();
                CategoriesWP _part = (CategoriesWP)WebPartToEdit;
                if (_part._BlogId > -1)
                {
                    var _Blogs = SiteFox.Business.Manager.Blog.GetByID(_part._BlogId);
                    if (_Blogs != null)
                    {
                        _ddl_BlogList.SelectedValue = _part._BlogId.ToString();
                    }
                }
                _tb_ViewByCategoryUrl.Text = _part._ViewByCategoryUrl;
                _tb_XsltUrl.Text = _part._XsltUrl;
            }
            protected override void CreateChildControls()
            {
                Controls.Clear();
                _lit_Brake = new Literal();
                _lit_Brake.Text = "<br />";
                _lab_BlogList = new Label();
                _lab_BlogList.Text = "Blogs";
                _lab_BlogList.Width = Unit.Percentage(95);
                Controls.Add(_lab_BlogList);
                Controls.Add(_lit_Brake);
                _ddl_BlogList = new DropDownList();
                _ddl_BlogList.Width = Unit.Percentage(95);
                _ddl_BlogList.DataSource = SiteFox.Business.Manager.Blog.GetAll();
                _ddl_BlogList.DataTextField = "Name";
                _ddl_BlogList.DataValueField = "ID";
                _ddl_BlogList.DataBind();
                Controls.Add(_ddl_BlogList);
                Controls.Add(_lit_Brake);
                _lab_ViewByCategoryUrl = new Label();
                _lab_ViewByCategoryUrl.Text = "View url";
                _lab_ViewByCategoryUrl.Width = Unit.Percentage(95);
                Controls.Add(_lab_ViewByCategoryUrl);
                Controls.Add(_lit_Brake);
                _tb_ViewByCategoryUrl = new TextBox();
                _tb_ViewByCategoryUrl.Width = Unit.Percentage(95);
                Controls.Add(_tb_ViewByCategoryUrl);
                Controls.Add(_lit_Brake);
                _lab_XsltUrl = new Label();
                _lab_XsltUrl.Text = "XSL-T url";
                _lab_XsltUrl.Width = Unit.Percentage(95);
                Controls.Add(_lab_XsltUrl);
                Controls.Add(_lit_Brake);
                _tb_XsltUrl = new TextBox();
                _tb_XsltUrl.Width = Unit.Percentage(95);
                Controls.Add(_tb_XsltUrl);
            }
        }
    }
}
